#include "stdafx.h"
#include "UsedItemMonitorEx.h"
#include "UsedItemMonitorExDlg.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#endif

class CAboutDlg : public CDialog
{
	public		:
		
				CAboutDlg( VOID );

		enum { IDD = IDD_ABOUTBOX };

	protected	:

		virtual
		VOID	DoDataExchange( CDataExchange * pDX );

		DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg( VOID ) :
	CDialog( CAboutDlg::IDD )
{
}

VOID CAboutDlg::DoDataExchange( CDataExchange * pDX )
{
	CDialog::DoDataExchange( pDX );
}

BEGIN_MESSAGE_MAP( CAboutDlg, CDialog )
END_MESSAGE_MAP()

CUsedItemMonitorExDlg::CUsedItemMonitorExDlg( CWnd * pParent /* = NULL */ ) :
	CDialog( CUsedItemMonitorExDlg::IDD, pParent )
{
	m_hIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME );
}

VOID CUsedItemMonitorExDlg::DoDataExchange( CDataExchange * pDX )
{
	CDialog::DoDataExchange( pDX );
	DDX_Control(pDX, IDC_BTN_DO_START_MONITORING, m_btnStartMonitoring);
	DDX_Control(pDX, IDC_EDIT_CYCLE, m_editCycle);
	DDX_Control(pDX, IDC_EDIT_KEYWORD, m_editKeyWords);
}

BEGIN_MESSAGE_MAP( CUsedItemMonitorExDlg, CDialog )
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	//}}AFX_MSG_MAP
	ON_BN_CLICKED(IDC_BTN_DO_START_MONITORING, &CUsedItemMonitorExDlg::OnBnClickedBtnDoStartMonitoring)
	ON_EN_CHANGE(IDC_EDIT_CYCLE, &CUsedItemMonitorExDlg::OnEnChangeEditCycle)
	ON_EN_CHANGE(IDC_EDIT_KEYWORD, &CUsedItemMonitorExDlg::OnEnChangeEditKeyword)
END_MESSAGE_MAP()


BOOL CUsedItemMonitorExDlg::OnInitDialog( VOID )
{
	CDialog::OnInitDialog();

	ASSERT( ( IDM_ABOUTBOX & 0xFFF0 ) == IDM_ABOUTBOX );
	ASSERT( IDM_ABOUTBOX < 0xF000 );

	CMenu * pSysMenu = GetSystemMenu( FALSE );
	if( pSysMenu != NULL )
	{
		CString strAboutMenu;
		strAboutMenu.LoadString( IDS_ABOUTBOX );
		if( !strAboutMenu.IsEmpty() )
		{
			pSysMenu->AppendMenu( MF_SEPARATOR );
			pSysMenu->AppendMenu( MF_STRING, IDM_ABOUTBOX, strAboutMenu );
		}
	}

	SetIcon( m_hIcon, TRUE );
	SetIcon( m_hIcon, FALSE );

	return this->Initialize();
}

VOID CUsedItemMonitorExDlg::OnSysCommand( UINT nID, LPARAM lParam )
{
	if( ( nID & 0xFFF0 ) == IDM_ABOUTBOX )
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand( nID, lParam );
	}
}

VOID CUsedItemMonitorExDlg::OnPaint( VOID )
{
	if( IsIconic() )
	{
		CPaintDC dc( this );

		SendMessage( WM_ICONERASEBKGND, reinterpret_cast< WPARAM >( dc.GetSafeHdc() ), 0 );

		int cxIcon = GetSystemMetrics( SM_CXICON );
		int cyIcon = GetSystemMetrics( SM_CYICON );

		CRect rect;
		GetClientRect( &rect );
		int x = ( rect.Width() - cxIcon + 1 ) / 2;
		int y = ( rect.Height() - cyIcon + 1 ) / 2;

		dc.DrawIcon( x, y, m_hIcon );
	}
	else
	{
		CDialog::OnPaint();
	}
}

HCURSOR CUsedItemMonitorExDlg::OnQueryDragIcon( VOID )
{
	return static_cast< HCURSOR >( m_hIcon );
}

BOOL CUsedItemMonitorExDlg::Initialize( VOID )
{
	
	return TRUE;
}

VOID CUsedItemMonitorExDlg::OnBnClickedBtnDoStartMonitoring( VOID )
{
	DWORD				dwCycle;
	deque< tstring >	dequeKeyWords;
	if( this->GetParam( dwCycle, dequeKeyWords ) &&
		this->m_NaverCafeKeyWordMonitor.Initialize( dwCycle, dequeKeyWords ) )
	{

	}
}

VOID CUsedItemMonitorExDlg::CheckParam( VOID )
{
	BOOL bEnable = FALSE;

	CString sCycle, sKeyWords;
	if( this->GetParam( sCycle, sKeyWords ) )
		if( sCycle.GetLength() &&
			sKeyWords.GetLength() )
			bEnable = TRUE;

	if( this->m_btnStartMonitoring.GetSafeHwnd() )
		this->m_btnStartMonitoring.EnableWindow( bEnable );
}

BOOL CUsedItemMonitorExDlg::GetParam( CString & sCycle, CString & sKeyWords )
{
	if( !this->GetSafeHwnd() )
		return FALSE;

	if( !this->m_editCycle.GetSafeHwnd() ||
		!this->m_editKeyWords.GetSafeHwnd() )
		return FALSE;

	this->m_editCycle.GetWindowText(	sCycle		);
	this->m_editKeyWords.GetWindowText(	sKeyWords	);
	
	return TRUE;
}

BOOL CUsedItemMonitorExDlg::GetParam( DWORD & dwCycle, deque< tstring > & dequeKeyWords )
{
	CString sCycle, sKeyWords;
	if( !this->GetParam( sCycle, sKeyWords )	||
		!sCycle.GetLength()						||
		!sKeyWords.GetLength() )
		return FALSE;

	dwCycle = ( DWORD )( _tstoi( sCycle.GetBuffer() ) * 1000 );
	sCycle.ReleaseBuffer();

	tstring sKeyWord( sKeyWords.GetBuffer() );
	sKeyWords.ReleaseBuffer();

	return STLUtil::Split( sKeyWord, dequeKeyWords, L"," ) > 0 ? TRUE : FALSE;
}

VOID CUsedItemMonitorExDlg::OnEnChangeEditCycle( VOID )
{
	this->CheckParam();
}

VOID CUsedItemMonitorExDlg::OnEnChangeEditKeyword( VOID )
{
	this->CheckParam();
}
